package com.qiuling.iot.server;

import com.alibaba.fastjson.JSONObject;
import com.qiuling.iot.constants.Constant;
import com.qiuling.iot.entity.SocketServer;
import com.qiuling.iot.vo.req.ServerSendReqVO;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author：zhangqiang
 * @Description：
 * @Date：Created in 20:34 2020/2/23
 * @Modified By：
 */
@Slf4j
public class ListeningThread extends Thread {
    private SocketServer socketServer;

    private ServerSocket serverSocket;

    private boolean isRunning;


    public ListeningThread(SocketServer socketServer) {
        this.socketServer = socketServer;
        this.serverSocket = socketServer.getServerSocket();
        isRunning = true;
        log.info("socket服务端开始监听");
    }

    @Override
    public void run() {
        while (isRunning) {
            if (serverSocket.isClosed()) {
                isRunning = false;
                break;
            }
            try {
                Socket socket;
                socket = serverSocket.accept();
                if (socketServer.getExistConnectionThreadList().size() > Constant.MAX_SOCKET_THREAD_NUM) {
                    //超过线程数量
                    PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
                    ServerSendReqVO vo = new ServerSendReqVO();
                    vo.setStatusCode(999);
                    vo.setErrorMessage("已超过连接最大数限制，请稍后再试");
                    writer.println(JSONObject.toJSONString(vo));
                    socket.close();
                }
                //设置超时时间为5s（有心跳机制了不需要设置）
                //				socket.setSoTimeout(5 * 1000);
                ConnectionThread connectionThread = new ConnectionThread(socket, socketServer);
                socketServer.getExistConnectionThreadList().add(connectionThread);
                //todo:这边最好用线程池
                connectionThread.start();
            } catch (IOException e) {
                log.error("ListeningThread.run failed,exception:{}", e.getMessage());
            }
        }
    }

    /**
     * 关闭所有的socket客户端连接的线程
     */
    public void stopRunning() {
        for (ConnectionThread currentThread : socketServer.getExistConnectionThreadList()) {
            currentThread.stopRunning();
        }
        isRunning = false;
    }
}
